﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using DAL;

namespace PeisongWeb.admin
{
    public partial class departmentList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("AI", 1))
                {
                    rptBind();
                    ddlpdtBind();
                    ddlcdtBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }

        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (ddl_cdt.SelectedValue == "")
            {
                if (ddl_pdt.SelectedValue != "")
                {
                    where.AppendFormat(" and dId={0}", Convert.ToInt32(ddl_pdt.SelectedValue));
                    where.AppendFormat(" or parentId={0}", Convert.ToInt32(ddl_pdt.SelectedValue));
                }
            }
            else
            {
                where.AppendFormat(" and dId={0}", Convert.ToInt32(ddl_cdt.SelectedValue));
            }
            DataSet ds = logic.departmentType.select(where.ToString(), "isShow DESC,sort", 100, 1);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        private void categoryRootBind()
        {
            ddl_parentDepartment.DataSource = logic.departmentType.listWithParent();
            ddl_parentDepartment.DataBind();
            ddl_parentDepartment.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", "0"));
        }

        private void ddlpdtBind()
        {
            ddl_pdt.DataSource = logic.departmentType.listWithParent();
            ddl_pdt.DataBind();
            ddl_pdt.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }
        private void ddlcdtBind()
        {
            if (ddl_pdt.SelectedValue != "")
            {
                ddl_cdt.DataSource = logic.departmentType.getByParent(Convert.ToInt32(ddl_pdt.SelectedValue));
                ddl_cdt.DataBind();
                ddl_cdt.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
            else 
            {
                ddl_cdt.Items.Clear();
                ddl_cdt.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }
        protected void ddl_pdt_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlcdtBind();
        }

        protected void lbn_select_Click(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbn_reset_Click(object sender, EventArgs e)
        {
            ddl_cdt.SelectedValue = "";
            ddl_pdt.SelectedValue = "";
            rptBind();
        }
        protected void btn_addDepartment_Click(object sender, EventArgs e)
        {
            categoryRootBind();
            txt_DepartmentName.Text = "";
            txt_sort.Text = "1";
            hdtype.Value = "Add";
            departmentdetail.Show();
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                HiddenField hdisShow=(HiddenField)e.Item.FindControl("hdisShow");
                int dId = Convert.ToInt32(e.CommandArgument.ToString());
                if (hdisShow.Value == "True")
                {
                    logic.departmentType.deleteWithParent(dId);
                    ddlpdtBind();
                    ddlcdtBind();
                    ddl_cdt.SelectedValue = "";
                    ddl_pdt.SelectedValue = "";
                    
                    rptBind();
                    ScriptManager.RegisterStartupScript(UP1, this.GetType(), "alert", "alert('禁用成功！')", true);
                }
                else
                {
                    DepartmentTypeEntity dtEnt = logic.departmentType.getByChildId(dId);
                    if (dtEnt.IsShow == false)
                    {
                        ScriptManager.RegisterStartupScript(UP1, this.GetType(), "alert", "alert('该部门的上级部门还是禁用状态，请先启用该部门的上级部门！')", true);
                    }
                    else
                    {
                        logic.departmentType.updateIsShow(dId);
                        ddlpdtBind();
                        ddlcdtBind();
                        ddl_cdt.SelectedValue = "";
                        ddl_pdt.SelectedValue = "";
                        
                        rptBind();
                        ScriptManager.RegisterStartupScript(UP1, this.GetType(), "alert", "alert('启用成功！')", true);
                    }
                }

            }
            if (e.CommandName == "Edit")
            {
                int dId = Convert.ToInt32(e.CommandArgument.ToString());
                DepartmentTypeEntity de = logic.departmentType.getById(dId);
                categoryRootBind();
                txt_DepartmentName.Text = de.DepartmentType;
                ddl_parentDepartment.SelectedValue = de.ParentId.ToString();
                txt_sort.Text = de.Sort.ToString();
                hdtype.Value = "Edit";
                hddid.Value = dId.ToString();
                departmentdetail.Show();
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (hdtype.Value == "Add")
            {
                logic.departmentType.insertAsChild(Convert.ToInt32(ddl_parentDepartment.SelectedValue), txt_DepartmentName.Text, Convert.ToInt32(txt_sort.Text));
                txt_DepartmentName.Text = "";
                txt_sort.Text = "1";
                Library.Script.ClientMsg("新增成功！");
                ddlpdtBind();
                ddlcdtBind();
                ddl_cdt.SelectedValue = "";
                ddl_pdt.SelectedValue = "";
                
                rptBind();
                departmentdetail.Close();
            }
            if (hdtype.Value == "Edit")
            {
                logic.departmentType.update(Convert.ToInt32(hddid.Value), txt_DepartmentName.Text, Convert.ToInt32(ddl_parentDepartment.SelectedValue), Convert.ToInt32(txt_sort.Text));
                Library.Script.ClientMsg("修改成功！");
                ddlpdtBind();
                ddlcdtBind();
                ddl_cdt.SelectedValue = "";
                ddl_pdt.SelectedValue = "";
                
                rptBind();
                departmentdetail.Close();
            }
        }

    }
}
